package com.goldcn.classify.controller;

import com.goldcn.classify.dto.ClassifyStyleDto;
import com.goldcn.classify.model.Classify;
import com.goldcn.classify.model.ClassifyStyle;
import com.goldcn.classify.model.Style;
import com.goldcn.classify.service.ClassifyService;
import com.goldcn.classify.service.ClassifyStyleService;
import com.goldcn.classify.service.StyleService;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/9.
 */
@RestController
@RequestMapping(value = "/classifystyle")
public class ClassifyStyleController {
    @Autowired
    private ClassifyStyleService classifyStyleService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private StyleService styleService;

    @RequestMapping(method = RequestMethod.POST)
    public Object add(@Valid @RequestBody ClassifyStyle record){
        return classifyStyleService.insertSelective(record);
    }

    /**
     * add Classify_Style
     * @param list
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object addlist(@RequestBody ClassifyStyleDto list){
        if(classifyStyleService.addOrUpdatelist(list)){
            return "success";
        }else{
            return "error";
        }
    }

    /**
     * 添加品类和属性关联
     * @param list
     * @return
     */
    @RequestMapping(value = "/cslist",method = RequestMethod.POST)
    public Object addlist2(@RequestBody Map<String,Object> list){
        return classifyStyleService.insertlist((List<Map<String,String>>)list.get("cslist"));
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Object delete(@PathVariable String id){
        classifyStyleService.selectByPrimaryKey(id);
        return classifyStyleService.deleteByPrimaryKey(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object loadByPcID(@PathVariable String id){
        return classifyStyleService.selectByPrimaryKey(id);
    }

    @RequestMapping(value = "/relistByClassify/{id}",method = RequestMethod.GET)
    public Object loadByClassify(@PathVariable String id){
        return classifyStyleService.loadByClassify(id);
    }

    @RequestMapping(value = "/clazzs/{styleid}",method = RequestMethod.GET)
    public Object loadClassify(@PathVariable String styleid){

        return classifyStyleService.loadClassify(styleid);
    }

    /**
     * 删除品类和款式关联
     * @param map
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public Object delete(@RequestBody Map<String,String> map){
        List<Map<String,Object>> csidlist=classifyStyleService.loadCsIDByClassify(map.get("clazzid"));
        int times=0;
        for(Map<String,Object> csid:csidlist){
            if(csid.get("gd_styleID").equals(map.get("styleid"))){
                classifyStyleService.deleteByPrimaryKey(csid.get("gd_csID").toString());
                times++;
            }
        }
        if(times==0){
            return false;
        }else{
            return true;
        }
    }
}
